package Multithread.Thread;

/**
 * 3 守护线程
 *
 */
public class Thread03 {

    public static void main(String[] args) throws InterruptedException {

//        1.守护线程
//        fun01();


    }

    /**
     * 1.守护线程
     * 只要其他线程运行结束，即使守护线程的代码没有执行结束，也会强制结束
     * 垃圾回收器就是一种守护线程
     * tomcat中的Acceptor和Poller就是一种守护线程
     */
    public static void fun01() throws InterruptedException {
        System.out.println("this is fun01");
//        守护线程
        Thread t1 = new Thread(()->{
           for ( int i = 0; i <= 10 ;i++) {
               System.out.println("线程t1 : " + i);
           }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"t1");

        Thread t2 = new Thread(()->{
            int j = 0 ;
            while (true) {
                System.out.println("线程t2 is 守护线程 : " + j++ );
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"t2");

//        设置为守护线程
        t2.setDaemon(true);

        t1.start();
        t2.start();
        Thread.sleep(1);
        System.out.println("守护线程终止");

    }


}
